﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using EssenceFeed.Data.EntityModel;
using EssenceFeed.Data.Interfaces;

namespace EssenceFeed.Data.Repositories
{
    /// <summary>
    /// Customer Repository
    /// </summary>
    public class CustomerRepository : ICustomerRepository
    {
        readonly EssenceFeedEntities _db = new EssenceFeedEntities();

        /// <summary>
        /// Get Customer Details by Customer Name
        /// </summary>
        /// <param name="strQuery"></param>
        /// <returns></returns>
        public IQueryable<Customer> FindCustomerByName(string strQuery)
        {
            return _db.Customers.Where(d => d.FirstName.Contains(strQuery));
        }

        /// <summary>
        /// Get All Templates
        /// </summary>
        /// <returns></returns>
        public IQueryable<Customer> FindAllCustomer()
        {
            return _db.Customers;
        }

        /// <summary>
        /// Get Customer by Id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Customer GetCustomerById(int id)
        {
            return _db.Customers.SingleOrDefault(d => d.CustomerId == id);
        }

        /// <summary>
        /// Is Customer Exists or Not
        /// </summary>
        /// <param name="email"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Customer IsCustomerExistByEmail(string email)
        {
            return _db.Customers.SingleOrDefault(d => d.Email == email);
        }

        /// <summary>
        /// Add Customer in the Database
        /// </summary>
        /// <param name="customer"></param>
        public void Add(Customer customer)
        {
            _db.Customers.AddObject(customer);
        }

        /// <summary>
        /// Delete Customer from Database
        /// </summary>
        /// <param name="customer"></param>
        public void Delete(Customer customer)
        {
            _db.Customers.DeleteObject(customer);
        }

        /// <summary>
        /// Save Customer in the database
        /// </summary>
        public void Save()
        {
            _db.SaveChanges();
        }
    }
}